package com.wyx.videoplay;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wyx.videoplay.common.Constants;
import com.wyx.videoplay.utils.Remember;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class LoginActivity extends AppCompatActivity {

    Toolbar mToolbar;
//    @BindView(R.id.iv_change_account_type_act_login)
//    ImageView mIvChangeAccountType;
    EditText mEtUsername;
    EditText mEtPassword;
    Button mBtnLogin;
    TextView mTvAccountType;
    TextView mActLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        settingIp();
        setContentView(R.layout.activity_login);

        mToolbar=findViewById(R.id.toolbar_act_login);
        mEtUsername=findViewById(R.id.et_username_act_login);
        mEtPassword=findViewById(R.id.et_password_act_login);
        mBtnLogin=findViewById(R.id.btn_login_act_login);
        mTvAccountType=findViewById(R.id.tv_account_type_act_login);
        mActLogin=findViewById(R.id.tv_app_name_act_login);

        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        getSupportActionBar().setTitle("登录");





        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(getString(mEtUsername))){
                    showToast("帐号不能为空！");
                }else if(TextUtils.isEmpty(getString(mEtPassword))){
                    showToast("密码不能为空！");
                }else {
                    login(getString(mEtUsername),getString(mEtPassword));
                }
            }
        });
        /*mActLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                去注册
                startActivity(new Intent(LoginActivity.this,M.class));



            }
        });*/

        if(!TextUtils.isEmpty(Remember.getString(Constants.USER_NAME,""))){
            startActivity(new Intent(this,MainActivity.class));
            finish();
        }

    }


    private void login(String acount, String pwd){
        String url = Constants.BASE_URL + "/api/information/login";
        /*LoginParmsEntity register = new LoginParmsEntity();
        register.setAccount(getString(mEtUsername));
        register.setPassword(getString(mEtPassword));

        HttpInfo build = HttpInfo.Builder().setUrl(url).setRequestType(RequestType.POST)
                .addParamJson(GsonUtil.gsonToJson(register))
                .build();
        com.okhttplib.OkHttpUtil.getDefault(this).doAsync(build, new Callback() {
            @Override
            public void onSuccess(HttpInfo info) throws IOException {
                Type type=new TypeToken<LoginResult<DataBean>>(){}.getType();
                LoginResult<DataBean> detail = info.getRetDetail(type);
                Log.d("wuyunxiang",detail.toString());
                if(detail.getData()!=null){
                    Remember.putString(Constants.USER_ACOUNT,detail.getData().getAccount());
                    Remember.putString(Constants.USER_NAME,detail.getData().getUser_name());
                    showToast("登录成功!");
                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    finish();
                }else {
                    showToast(detail.getMessage());
                }

            }

            @Override
            public void onFailure(HttpInfo info) throws IOException {

                Type type=new TypeToken<LoginResult>(){}.getType();
                LoginResult detail = info.getRetDetail(type);
                Log.d("wuyunxiang",detail.getMessage());
                showToast(detail.getMessage());
            }
        });*/
    }

    protected static String getString(EditText et) {
        return et.getText().toString();
    }

    public void showToast(String text) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }

}
